/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-1-4 21:53:17
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#ifndef INCLUDE_NET_NETINSIDE_NR_SOCKET_H_
#define INCLUDE_NET_NETINSIDE_NR_SOCKET_H_

#include "sockbase.h"
#include "netbuffer.h"

#include <map>

class NRSocket : public sockbase_t, public smart::refobj_t
{
	NRSocket( const NRSocket& );
	NRSocket& operator= ( const NRSocket& );
public:
	NRSocket();
protected:
	virtual ~NRSocket(); //should not to delete

public:
	void reset();

	int send_buffer_size();
	int push_row_to_send_buffer_back(net_buffer_row_t* row);
	int push_row_to_send_buffer_front(net_buffer_row_t* row);
	smart::ref_ptr<net_buffer_row_t> pop_row_from_send_buffer_back();
	smart::ref_ptr<net_buffer_row_t> pop_row_from_send_buffer_front();

	int recv_buffer_size();
	int push_row_to_recv_buffer_back(net_buffer_row_t* row);
	int push_row_to_recv_buffer_front(net_buffer_row_t* row);
	smart::ref_ptr<net_buffer_row_t> pop_row_from_recv_buffer_back();
	smart::ref_ptr<net_buffer_row_t> pop_row_from_recv_buffer_front();

public:
    void lock_send();
    void unlock_send();
    
    void lock_recv();
    void unlock_recv();

protected:
	void clear_send_buffer();
	void clear_recv_buffer();
public:
	char m_ip[128];
	int m_port;
protected:
	NetBufferRowQueue m_buffer_send_Q; //send buffer
	NetBufferRowQueue m_buffer_recv_Q; //recv buffer
	smart::mutex_t m_mtx_buffer_send;
	smart::mutex_t m_mtx_buffer_recv;

//	bool m_tag; //是否被一个线程标记,上层处理器正在从缓存中接收数据
//	smart::mutex_t m_mtx_tag;
    
    smart::mutex_t m_send_lock;
    smart::mutex_t m_recv_lock;
};

typedef std::map< int, smart::ref_ptr<NRSocket> >    NetConnMap;
typedef std::map< int, smart::ref_ptr<NRSocket> >    NetListenMap;

#endif //~INCLUDE_NET_NETINSIDE_NR_SOCKET_H_

